<?php
/**
* @todo pull up class to grid package
*/
class Shuffler_Mapper_Paginating extends Shuffler_Mapper
{

    protected function listSelect( $offset = 0, $limit = 10 )
    {
        if( is_null( $offset ) )
        {
            $offset = 0;
        }
        if( is_null( $limit ) )
        {
            $limit = 10;
        }
        return $this->select()
            ->limit( $limit, $offset );
    }
    
    public function findAll( $offset = null, $limit = null )
    {
        $query = $this->doList( $offset, $limit);
        return $this->query( $query );
    }
    
    protected function listWhere( $where, $offset = null, $limit = null )
    {
        $query = $this->doList( $offset, $limit)
            ->where( $where );
        return $this->query( $query );
    }
                                 
    protected function doList( $offset = null, $limit = null ) 
    {
        return $this->listSelect( $offset, $limit);
    }
    

    
    // search / filter methods
    
    /**
    * Find row models where table field name matches given value
    * 
    * @param string Name of field
    * @param value to match
    * @return collection of Shuffler_Model
    */
    public function listByField( $fieldName, $value, $offset = null, $limit = null )
    {
        $db = $this->getReadAdapter();
        $where = $db->quoteInto( $db->quoteIdentifier( $fieldName ) . ' = ?', $value );
        return $this->listWhere( $where, $offset, $limit  );
    }
    
    /**
    * Finds titles that match a string for auto-completion
    */
    public function findTitlesLike( $match )
    {
        $select = $this->doSelect();
        $select->columns( 'title, count( * ) as c' );
        $select->where( "title LIKE ?", '%' . $match . '%' );
        $select->group( 'title' );
        $select->order( 'c DESC' );
        $select->limit( 10 );
        
        $titles = array();
        foreach( $this->doQuery( $this->getReadAdapter(), $select )->fetchAll() as $row )
        {
            array_push( $titles, $row['title'] );
        }
        return $titles;
    }
    
    /**
    * Perform search
    */
    public function search( $q, $offset = NULL, $limit = 7 )
    {
        $select = $this->select();
        $select->where( "title LIKE ?", '%' . $q . '%' );
        $select->limit( $limit, $offset );  
        return $this->query( $select );
    }
    
    /**
    * count search
    */
    public function countSearch( $q )
    {
        return $this->countWhere( "title LIKE ?", '%' . $q . '%' );
    }
    
    /**
    * Find Educators that fulfill a conjunction of criterion
    * 
    * @return Shuffler_Collection
    */
    public function filter( array $filters, $offset = NULL, $limit = 7  )
    {
        $select = $this->filterSelect();
        foreach( $filters as $filter )
        {
            if( !$filter->isActive() )
            {
                continue;
            }
            $filter->from( $select );
            $select->where( $filter->where() ); 
            $filter->group( $select ); 
        }

        $select->limit( $limit, $offset  );
        return $this->query( $select );
    }
    
    /**
    * Builds a base select statement,
    * complete with the specific fields to select
    * 
    * @return Zend_Db_Select
    */
    protected function filterSelect()
    {
        $select = $this->doFilterSelect();
        $select = $this->doSelectFields( $select );
        return $select;
    }
       
    /**
    * Builds a base select statement with no columns / select part
    * used for adding on aggregrate / other "sugar"
    * 
    * @return Zend_Db_Select   
    */
    protected function doFilterSelect()
    {
        $select = $this->doSelect();
        return $select;
    }
        
    /**
    * count Educators that fulfill a conjunction of criterion
    * 
    * @return integer
    */
    public function filterCount( array $filters  )
    {
        $select = $this->doFilterSelect();
        foreach( $filters as $filter )
        {
            if( !$filter->isActive() )
            {
                continue;
            }
            $filter->from( $select );
            $select->where( $filter->where() ); 
        }
        $cols = $this->selectCols( 'count( distinct( ' . $this->getTableName() . '.' . $this->primaryKey() . ' ) )', $select );
        return $cols;
    }
    
    
    
    
}